package api.cie.rss.adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import api.cie.rss.MainActivity;
import api.cie.rss.R;

public class FileListAdapter extends BaseAdapter {

	private List<String> files;
	private List<String> selectedFiles;
	private LayoutInflater mInflater;
	
	public FileListAdapter(Context ctx) {
		super();
		files = new ArrayList<String>();
		selectedFiles = new ArrayList<String>();
		mInflater = LayoutInflater.from(ctx);
	}
	
	public List<String> getSelectedFiles() {
		return selectedFiles;
	}
	
	public void setFiles(List<String> files) {
		this.files = files;
	}
	
	@Override
	public int getCount() {
		return files.size();
	}

	@Override
	public Object getItem(int pos) {
		return files.get(pos);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		view = mInflater.inflate(R.layout.file_row, parent, false);
		
		final int pos = position;
		
		TextView tvFile = (TextView)view.findViewById(R.id.textFile);
		tvFile.setText(files.get(position));
		tvFile.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(), MainActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("fileName", ((TextView)v).getText().toString());
				intent.putExtras(bundle);
				v.getContext().startActivity(intent);
			}
		});
		
		CheckBox chkFile = (CheckBox)view.findViewById(R.id.checkFile);
		chkFile.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(((CheckBox)v).isChecked()) {
					selectedFiles.add(files.get(pos));
				} else {
					selectedFiles.remove(files.get(pos));
				}
			}
		});
		
		return view;
	}
	
	
	
}
